package com.example.myapplicationimagelib;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private EditText et_path;
    private ImageView iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linearlayout);
        et_path = (EditText)  findViewById(R.id.ed_path);
        iv = (ImageView) findViewById(R.id.iv_image);
        et_path.setText("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1511956400&di=dded5dedb51f473d9ebb4bc8b09f9585&imgtype=jpg&er=1&src=http%3A%2F%2Fp5.zbjimg.com%2Ftask%2F2012-05%2F25%2Fpub%2F4fbe7632b0df1.jpg");
    }
    public void click(Button v){

        try{
            // 获取图片路径
            String path = et_path.getText().toString();
            URL url = new URL(path);
           HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            int code = connection.getResponseCode();
            if (code == 200){
                // 获取图片数据
                InputStream in = connection.getInputStream();

                // [2.8] 通过地图工厂获取bitmap
                Bitmap bitmap = BitmapFactory.decodeStream(in);

                // 显示到iv
                iv.setImageBitmap(bitmap);

            }
        }catch (Exception e)
        {

        }

    };

}
